package com.zoe.app.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


/**
 * @author Zoe.Zhang
 * @date 2021/10/16
 * @description
 */
@Configuration
public class DataSourceConfig {
    @Bean(value = "master")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    DataSource masterDataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        return hikariDataSource;
    }

    @Bean(value = "slave")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    // https://www.jianshu.com/p/68a75c093023
    @ConditionalOnProperty(prefix = "spring.datasource.slave", name = "enable", havingValue = "true")
    DataSource slaveDataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        return hikariDataSource;
    }
}
